package com.dragon.gencode.test;

import com.dragon.gencode.conf.db.DynamicDataSourceContextHolder;
import com.dragon.gencode.conf.db.TargetDataSource;
import com.dragon.gencode.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @ResponseBody
    @GetMapping("/")
    public Map<String, Object> map() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM dt_action_category");
        return list.get(0);
    }

    @ResponseBody
    @GetMapping("/db2")
    @TargetDataSource(DynamicDataSourceContextHolder.DataSourceKey.GEN)
    public List<Map<String, Object>> deb2() {
        StringBuilder sb = new StringBuilder();
        sb.append("select  table_name, table_comment, create_time, update_time from information_schema.tables where table_schema = (select database());");
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
//        List<GenTable> tables = BeanUtils.toBeanList(maps, GenTable.class, k -> StringUtils.toCamelCase(k));
        return list;
    }


    @ResponseBody
    @GetMapping("/db1")
    @TargetDataSource(DynamicDataSourceContextHolder.DataSourceKey.GEN_CONF)
    public List<Map<String, Object>> db1() {
        StringBuilder sb = new StringBuilder();
        sb.append("select  table_name, table_comment, create_time, update_time from information_schema.tables where table_schema = (select database());");
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
//        List<GenTable> tables = BeanUtils.toBeanList(maps, GenTable.class, k -> StringUtils.toCamelCase(k));
        return list;
    }


    @ResponseBody
    @GetMapping("/columns")
    public List columns(String table) {
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT ");
        sb.append(" 	column_name, ");
        sb.append(" 	( CASE WHEN ( is_nullable = 'no'  &&  column_key != 'PRI' ) THEN '1' ELSE '0' END ) AS is_required, ");
        sb.append(" 	( CASE WHEN column_key = 'PRI' THEN '1' ELSE '0' END ) AS is_pk, ");
        sb.append(" 	ordinal_position AS sort, ");
        sb.append(" 	column_comment, ");
        sb.append(" 	( CASE WHEN extra = 'auto_increment' THEN '1' ELSE '0' END ) AS is_increment, ");
        sb.append(" 	column_type  ");
        sb.append(" FROM ");
        sb.append(" 	information_schema.COLUMNS  ");
        sb.append(" WHERE ");
        sb.append(" 	table_schema = (SELECT DATABASE())  ");
        sb.append(" 	AND table_name = ?  ");
        sb.append(" ORDER BY ");
        sb.append(" 	ordinal_position ");
        if (StringUtils.isEmpty(table)) {
            table = "dt_action_category";
        }
        String params[] = new String[]{table};
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString(), params);
//        List<GenTableColumn> list = jdbcTemplate.queryForList(sb.toString(), GenTableColumn.class, params);
        return list;
    }
}